﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using PGI.EN.Producto;

namespace PGI.AD.Producto
{
    public class ProductoAd
    {
        public static int InsertarProducto(ProductoEn _productoEn)
        {
            SqlCommand command = null;
            SqlConnection connection = null;
            SqlDataReader reader = null;

            string sql = "MntProductoI";

            try
            {
                connection = DataBaseFactory.SqlOpenDatabase("PGI");

                command = new SqlCommand(sql, connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@CategoriaID", _productoEn.CategoriaID);
                command.Parameters.AddWithValue("@MarcaID", _productoEn.MarcaID);
                command.Parameters.AddWithValue("@Codigo", _productoEn.Codigo);
                command.Parameters.AddWithValue("@Descripcion", _productoEn.Descripcion);                
                command.Parameters.AddWithValue("@CodBarra", _productoEn.CodBarra);
                command.Parameters.AddWithValue("@StockMin", _productoEn.StockMinimo);
                command.Parameters.AddWithValue("@Estado", _productoEn.Estado);
                command.Parameters.AddWithValue("@UsuarioID", _productoEn.UsuarioID);

                command.Parameters.Add("@Resultado", SqlDbType.Int).Direction = ParameterDirection.Output;

                command.ExecuteNonQuery();

                return Convert.ToInt32(command.Parameters["@Resultado"].Value);
            }

            catch (SqlException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            finally
            {
                if (reader != null)
                    reader.Close();
                DataBaseFactory.SqlCloseDatabase("PGI");
            }
        }

        public static int EliminarProducto(int _productoID, int _usuarioID)
        {
            SqlCommand command = null;
            SqlConnection connection = null;
            SqlDataReader reader = null;

            string sql = "MntProductoE";

            try
            {
                connection = DataBaseFactory.SqlOpenDatabase("PGI");

                command = new SqlCommand(sql, connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add("@ProductoID", SqlDbType.Int).Direction = ParameterDirection.Input;
                command.Parameters.Add("@UsuarioID", SqlDbType.Int).Direction = ParameterDirection.Input;
                command.Parameters.Add("@Resultado", SqlDbType.Int).Direction = ParameterDirection.Output;

                command.Parameters["@ProductoID"].Value = _productoID;
                command.Parameters["@UsuarioID"].Value = _usuarioID;

                command.ExecuteNonQuery();

                return Convert.ToInt32(command.Parameters["@Resultado"].Value);
            }

            catch (SqlException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            finally
            {
                if (reader != null)
                    reader.Close();
                DataBaseFactory.SqlCloseDatabase("PGI");
            }
        }

        public static List<ProductoEn> SeleccionarProductos()
        {
            SqlCommand command = null;
            SqlConnection connection = null;
            SqlDataReader reader = null;

            string sql = "MntProductoS";

            try
            {
                connection = DataBaseFactory.SqlOpenDatabase("PGI");

                command = new SqlCommand(sql, connection);
                command.CommandType = CommandType.StoredProcedure;

                reader = command.ExecuteReader();
                List<ProductoEn> lstProductos = new List<ProductoEn>();
                ProductoEn prodEn = null;

                while (reader.Read())
                {
                    prodEn = new ProductoEn();
                    prodEn.ProductoID = reader.IsDBNull(0) ? 0 : Convert.ToInt32(reader.GetValue(0));
                    prodEn.CategoriaID = reader.IsDBNull(1) ? 0 : Convert.ToInt32(reader.GetValue(1));
                    prodEn.Codigo = reader.IsDBNull(2) ? string.Empty : reader.GetValue(2).ToString();
                    prodEn.CategoriaDes = reader.IsDBNull(3) ? string.Empty : reader.GetValue(3).ToString();
                    prodEn.MarcaID = reader.IsDBNull(4) ? 0 : Convert.ToInt32(reader.GetValue(4));
                    prodEn.MarcaDes = reader.IsDBNull(5) ? string.Empty : reader.GetValue(5).ToString();
                    prodEn.Descripcion = reader.IsDBNull(6) ? string.Empty : reader.GetValue(6).ToString();
                    prodEn.CodBarra = reader.IsDBNull(7) ? string.Empty : reader.GetValue(7).ToString();
                    prodEn.StockMinimo = reader.IsDBNull(8) ? 0 : Convert.ToInt32(reader.GetValue(8));                    
                    prodEn.Estado = reader.IsDBNull(9) ? 0 : Convert.ToInt32(reader.GetValue(9));
                    prodEn.UsuarioID = reader.IsDBNull(10) ? 0 : Convert.ToInt32(reader.GetValue(10));

                    lstProductos.Add(prodEn);
                }

                return lstProductos;
            }

            catch (SqlException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            finally
            {
                if (reader != null)
                    reader.Close();
                DataBaseFactory.SqlCloseDatabase("PGI");
            }
        }

        public static int EditarProducto(ProductoEn _productoEn)
        {
            SqlCommand command = null;
            SqlConnection connection = null;
            SqlDataReader reader = null;

            string sql = "MntProductoU";

            try
            {
                connection = DataBaseFactory.SqlOpenDatabase("PGI");

                command = new SqlCommand(sql, connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@ProductoID", _productoEn.ProductoID);
                command.Parameters.AddWithValue("@CategoriaID", _productoEn.CategoriaID);
                command.Parameters.AddWithValue("@Codigo", _productoEn.Codigo);
                command.Parameters.AddWithValue("@Descripcion", _productoEn.Descripcion);                
                command.Parameters.AddWithValue("@CodBarra", _productoEn.CodBarra);
                command.Parameters.AddWithValue("@StockMin", _productoEn.StockMinimo);                
                command.Parameters.AddWithValue("@UsuarioID", _productoEn.UsuarioID);

                command.Parameters.Add("@Resultado", SqlDbType.Int).Direction = ParameterDirection.Output;

                command.ExecuteNonQuery();

                return Convert.ToInt32(command.Parameters["@Resultado"].Value);
            }

            catch (SqlException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            finally
            {
                if (reader != null)
                    reader.Close();
                DataBaseFactory.SqlCloseDatabase("PGI");
            }
        }        
    }
}
